package com.android.ailaoshan.index;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.android.ailaoshan.R;
import com.android.ailaoshan.camera.ManagerPicture;
import com.android.ailaoshan.index.config.MapConfigure;
import com.android.ailaoshan.index.listener.NavigationViewListener;
import com.android.ailaoshan.tools.BaseApplication;
import com.android.ailaoshan.user.LoginActivity;
import com.android.ailaoshan.voice.ManagerVoice;

public class MainActivity extends AppCompatActivity {

    private BaseApplication baseApplication;
    private Context mContext;
    private MapConfigure mapConfigure;

    private DrawerLayout mDrawerLayout;

    private long pressTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        baseApplication = BaseApplication.getInstance();
        mContext = MainActivity.this;
    }

    @Override
    protected void onStart(){
        super.onStart();

        // 侧边滑动栏
        mDrawerLayout =findViewById(R.id.drawLayout_container);

        baseApplication.addActivity(MainActivity.this);

        setViewListener();
    }

    @Override
    protected void onResume(){
        super.onResume();

        // MapView加载
        mapConfigure = new MapConfigure(mContext,MainActivity.this);
        mapConfigure.initMap();
        mapConfigure.mapListener();
    }

    /**
     * 显示顶部toolBar菜单
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.toolbar_top_menu,menu);
        return true;
    }

    /**
     * 设置控件监听
     */
    private void setViewListener(){

        // 顶部toolBar
        Toolbar toolbar = findViewById(R.id.toolBar_top_container);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        // 侧边滑动栏监听
        ActionBarDrawerToggle drawerToggle =
                new ActionBarDrawerToggle(MainActivity.this,mDrawerLayout,toolbar,
                        R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        mDrawerLayout.addDrawerListener(drawerToggle);
        drawerToggle.syncState();

        // 侧边滑动栏菜单项监听
        NavigationViewListener navigationViewListener =
                new NavigationViewListener(MainActivity.this,mContext);
        navigationViewListener.setNavigationListen();

    }

    /**
     * 关闭侧边滑动栏
     */
    public void closeDrawerLayout(){
        mDrawerLayout.closeDrawer(GravityCompat.START);
    }

    /**
     * 登录、退出按钮监听
     */
    public void btnLoginAndSign(View v){
        switch (v.getId()){
            case R.id.navigation_left_button1:{
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, LoginActivity.class);
                startActivity(intent);
                break;
            }
        }
    }

    /**
     * ToolBar菜单项点击监听
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.tooBar_top_menu_item1) {
            Toast.makeText(mContext,"点击了顶部菜单栏第一项",Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 重写onKeyDown方法
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
            if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
                closeDrawerLayout();
            }else{
                onExit();
            }
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

    /**
     * 退出应用
     */
    private void onExit(){
        if((System.currentTimeMillis() - this.pressTime) > 2000){
            Toast.makeText(this,"再按一次退出应用",Toast.LENGTH_SHORT).show();
            this.pressTime = System.currentTimeMillis();
        }else{
            baseApplication.removeAllActivity();
        }
    }

    /**
     * 界面跳转返回
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode){
            case 201:{
                ManagerPicture managerPicture = new ManagerPicture();
                managerPicture.donePicture(resultCode,baseApplication.getImageFile());
                break;
            }
            case 202:{
                ManagerVoice managerVoice = new ManagerVoice();
                managerVoice.doneVoice(resultCode,baseApplication.getVoiceFile());
                break;
            }
        }
    }

    @Override
    protected void onPause(){
        super.onPause();
        mapConfigure.onPause();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        mapConfigure.releaseSource();
        mapConfigure.onDestroy();
        mapConfigure = null;
    }

}
